/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller;

import com.thienloc.omap.entity.KqxsLink;
import com.thienloc.omap.session.KqxsLinkFacade;
import com.thienloc.omap.util.JsfUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class LotteryLinkBean extends AbstractCrudBean {

    @EJB
    private KqxsLinkFacade facade;
    private KqxsLink selected;
    private List<KqxsLink> list;
    private List<String> selectedDays;

    private String getOpenDays() {
        try {
            StringBuilder result = new StringBuilder();
            for (String day : selectedDays) {
                result.append(day);
                result.append(",");
            }
            result.deleteCharAt(result.length() - 1);
            return result.toString();
        } catch (Exception ex) {
            String error = "Lỗi! ";
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage(error, ex.getMessage());
            return null;
        }
    }

    public List<KqxsLink> getList() {
        return list;
    }

    public void setList(List<KqxsLink> list) {
        this.list = list;
    }

    public KqxsLink getSelected() {
        return selected;
    }

    public void setSelected(KqxsLink selected) {
        this.selected = selected;
    }

    public List<String> getSelectedDays() {
        return selectedDays;
    }

    public void setSelectedDays(List<String> selectedDays) {
        this.selectedDays = selectedDays;
    }

    @Override
    protected void validateForm() throws Exception {
    }

    @Override
    protected Logger getLogger() {
        return LoggerFactory.getLogger(LotteryLinkBean.class);
    }

    @Override
    protected void resetToCreateFormAction() throws Exception {
        list = facade.findAll();
        selected = new KqxsLink();
        selectedDays = new ArrayList<String>();
        edit = false;
    }

    @Override
    protected void findSelected(int id) throws Exception {
        selected = facade.find(id);
    }

    @Override
    protected void createSelected() throws Exception {
        selected.setTime2Open(getOpenDays());
        facade.create(selected);
        resetToCreateForm();
        JsfUtil.addSuccessMessage("Thành công!", "Đã thêm mới thành công một bản ghi");
    }

    @Override
    protected void editSelected() throws Exception {
        selected.setTime2Open(getOpenDays());
        facade.edit(selected);
        resetToCreateForm();
        JsfUtil.addSuccessMessage("Thành công!", "Đã cập nhật thành công một bản ghi");
    }

    @Override
    protected void removeSelected() throws Exception {
        Integer id = Integer.parseInt(JsfUtil.getRequestParameter("id"));
        selected = facade.find(id);
        facade.remove(selected);
        resetToCreateForm();
        JsfUtil.addSuccessMessage("Thành công!", "Đã xóa thành công một bản ghi");
    }

    @Override
    protected void prepareViewAction() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void prepareEditAction() throws Exception {
        edit = true;
        Integer id = Integer.parseInt(JsfUtil.getRequestParameter("id"));
        selected = facade.find(id);
        selectedDays = Arrays.asList(selected.getTime2Open().split(","));
    }
}
